package com.ruoyi.web.controller.pinko;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.PinkoCurrencyLog;
import com.ruoyi.system.domain.PinkoCurrencyfields;
import com.ruoyi.system.service.IPinkoCurrencyLogService;
import com.ruoyi.system.service.IPinkoCurrencyfieldsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PinkoCurrency;
import com.ruoyi.system.service.IPinkoCurrencyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 币种信息Controller
 * 
 * @author ruoyi
 * @date 2024-06-07
 */

@RestController
@RequestMapping("/system/currency")
public class PinkoCurrencyController extends BaseController
{
    @Autowired
    private IPinkoCurrencyService pinkoCurrencyService;
    @Autowired
    private IPinkoCurrencyfieldsService currencyFieldsService;
    @Autowired
    private IPinkoCurrencyLogService currencyLogService;

    /**
     * 查询币种信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:currency:list')")
    @GetMapping("/list")
    public TableDataInfo list(PinkoCurrency pinkoCurrency)
    {
        startPage();
        List<PinkoCurrency> list = pinkoCurrencyService.selectPinkoCurrencyList(pinkoCurrency);
        return getDataTable(list);
    }

    /**
     * 导出币种信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:currency:export')")
    @Log(title = "币种信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PinkoCurrency pinkoCurrency)
    {
        List<PinkoCurrency> list = pinkoCurrencyService.selectPinkoCurrencyList(pinkoCurrency);
        ExcelUtil<PinkoCurrency> util = new ExcelUtil<PinkoCurrency>(PinkoCurrency.class);
        util.exportExcel(response, list, "币种信息数据");
    }

    /**
     * 获取币种信息详细信息
     */
    @Anonymous
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        PinkoCurrency obj=pinkoCurrencyService.selectPinkoCurrencyById(id);
        List<PinkoCurrencyfields> arryBascic=currencyFieldsService.getByFatherId(id);
        obj.setArryBasicFields(arryBascic);
        return success(obj);
    }

    /**
     * 新增币种信息
     */
    @PreAuthorize("@ss.hasPermi('system:currency:add')")
    @Log(title = "币种信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PinkoCurrency pinkoCurrency)
    {
        int resultnum=pinkoCurrencyService.insertPinkoCurrency(pinkoCurrency);
        String action="CREATE";
        String actDescription="新增数据";

        SaveActionLog(pinkoCurrency,action,actDescription);
        return toAjax(resultnum);
    }
    public void saveBasicFields(PinkoCurrency currencydto) {
        List<PinkoCurrencyfields> createDTOArry = currencydto.getArryBasicFields();
        currencyFieldsService.deleteByFatherId(currencydto.getId());
        for (PinkoCurrencyfields item : createDTOArry
        ) {
            currencyFieldsService.insertPinkoCurrencyfields(item);
        }
    }
    public void SaveActionLog(PinkoCurrency dto, String action , String actDescription)
    {
        PinkoCurrencyLog dtolog =new PinkoCurrencyLog();
        dtolog.setCreateDate(dto.getUpdateDate());
        dtolog.setCreator(dto.getUpdater());
        dtolog.setCreatorName(dto.getUpdaterName());
        if(dto.getId()==null|| dto.getId().equals(""))
        {
          List<PinkoCurrency>  arry0=  pinkoCurrencyService.selectPinkoCurrencyList(dto);
          if(arry0.size()>0)
              dto.setId(arry0.get(0).getId());
        }
        dtolog.setFatherId(dto.getId());
        dtolog.setFatherId(dto.getId());
        dtolog.setBlocknetworkId( dto.getBlocknetworkId());
        dtolog.setNetworkIcon(dto.getNetworkIcon());
        dtolog.setTokenContract(dto.getTokenContract());
        dtolog.setIsMultichain(dto.getIsMultichain());
        dtolog.setCurrencyType(dto.getCurrencyType());
        dtolog.setCurrencyAccuracy(dto.getCurrencyAccuracy());
        dtolog.setBlockbrowserUrl(dto.getBlockbrowserUrl());
        dtolog.setBlockbrowserUrl(dto.getBlockbrowserUrl());
        dtolog.setIsIntroduction(dto.getIsIntroduction());
        dtolog.setChinese(dto.getChinese());//
        dtolog.setTraditionalchinese(dto.getTraditionalchinese());
        dtolog.setEnglish(dto.getEnglish());//
        dtolog.setAction(action);
        dtolog.setActiondescription(actDescription);
        currencyLogService.insertPinkoCurrencyLog(dtolog);
    }
    /**
     * 修改币种信息
     */
    @PreAuthorize("@ss.hasPermi('system:currency:edit')")
    @Log(title = "币种信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PinkoCurrency pinkoCurrency)
    {
        int result=pinkoCurrencyService.updatePinkoCurrency(pinkoCurrency);
        saveBasicFields(pinkoCurrency);
        SaveActionLog(pinkoCurrency,"更新数据","UPDATE");
        return toAjax(result);
    }

    /**
     * 删除币种信息
     */
    @PreAuthorize("@ss.hasPermi('system:currency:remove')")
    @Log(title = "币种信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (long idstr:ids) {
            PinkoCurrency data = pinkoCurrencyService.selectPinkoCurrencyById(idstr);
            if(data!=null&&data.getBlocknetworkId().toString().length()>0){
                data.setUpdaterName("");
                currencyFieldsService.deleteByFatherId(idstr);
                SaveActionLog(data,"DELETED","删除数据");
            }
        }
        return toAjax(pinkoCurrencyService.deletePinkoCurrencyByIds(ids));
    }
}
